package cn.jinjimi.app.custom.entity;

import android.content.Context;

import com.google.gson.annotations.SerializedName;
import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.store.DaoSession;
import cn.jinjimi.app.store.dao.ContactInfoDao;
import cn.jinjimi.app.store.dao.CustomerDao;
import cn.jinjimi.app.store.dao.LastFollowDao;
import cn.jinjimi.app.store.entity.custom.ContactInfo;
import cn.jinjimi.app.store.entity.custom.LastFollow;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import de.greenrobot.dao.DaoException;

/**
 * 客户资料
 * <p>
 * Created by sun on 2015/5/14.
 * <p/>
 * modify jinsen 2015/12/04
 * modify Roy 2016/1/23
 * 添加字段 性别 客户层级 客户类别
 * 删除过时字段phone,查看phones字段
 * 整理custom的字段,根据字段性质分类
 * <p/>
 */
public class Custom implements Serializable {

    /**
     * 客户类型 0 普通客户 1 准客户 2 已签单客户
     */
    public static final int TYPE_SOLDED_CUSTOMER = 2;
    public static final int TYPE_UNSOLD_CUSTOMER = 1;
    public static final int TYPE_NORMAL_CUSTOMER = 0;

    /**
     * 客户是否已置顶 0 未置顶 1 已置顶
     */
    public static final int TAG_HAS_TOP = 1;
    public static final int TAG_NORMAL = 0;

    /**
     * 客户性别 0 未设置 1 男 2 女
     */
    public static final int CUSTOM_GENDER_MAN = 1;
    public static final int CUSTOM_GENDER_WOMAN = 2;
    public static final int CUSTOM_GENDER_NOSET = 0;

    /**
     * 证件类型 0未设置 1身份证  3护照 4军官证 5港台同胞 10其他
     * 11户口簿  12士兵证  13驾驶执照  14返乡证  15组织机构代码
     * 16港澳通行证  17台湾通行证
     */
    public static final int CUSTOM_IDCODE_NONE = 0;
    public static final int CUSTOM_IDCODE_ID = 1;
    public static final int CUSTOM_IDCODE_PASSPORT = 3;
    public static final int CUSTOM_IDCODE_OFFICER = 4;
    public static final int CUSTOM_IDCODE_HKTAWIN = 5;
    public static final int CUSTOM_IDCODE_ELSE = 10;
    public static final int CUSTOM_IDCODE_PERMANENT = 11;
    public static final int CUSTOM_IDCODE_SOLDIER = 12;
    public static final int CUSTOM_IDCODE_DIREVER = 13;
    public static final int CUSTOM_IDCODE_HOME = 14;
    public static final int CUSTOM_IDCODE_ORGANIZATION = 15;
    public static final int CUSTOM_IDCODE_HKPASSPORT = 16;
    public static final int CUSTOM_IDCODE_TAIWANPASSPORT = 17;

    /**
     * 客户层级 0 未设置 1 A级 2 B级 3 C级 4 D级
     */
    public static final int CUSTOM_LEVEL_A = 1;
    public static final int CUSTOM_LEVEL_B = 2;
    public static final int CUSTOM_LEVEL_C = 3;
    public static final int CUSTOM_LEVEL_D = 4;
    public static final int CUSTOM_NOT_SET = 0;

    //客户编号 id
    @SerializedName("id")
    public Long customerId;

    //代理人SSOID
    @SerializedName("agent_id")
    public String ssoId;

    //客户名字
    @SerializedName("real_name")
    public String name = "";

    //客户头像地址
    @SerializedName("head_image_url")
    public String avatar = "";

    //性别 0 未设置 1 男 2 女
    @SerializedName("gender")
    public int gender = CUSTOM_GENDER_NOSET;

    // 生日类型(0:无年份公历，1：含年份公历，2：无年份农历，3：含年份农历)，默认为无年份公历
    @SerializedName("birthday_type")
    public int birthdayType;

    //生日
    @SerializedName("birthday")
    public String birthday = "";

    //客户标签 0或空为普通客户，1：准客户，2已签单客户
    @SerializedName("type")
    public int type;

    //用户级别，1:A,2:B,3:C,4:D
    @SerializedName("category")
    public int category;

    //客户标签 标签id，用","分隔 格式如: 1,2,3
    @SerializedName("label")
    public String label;

    //客户来源
    @SerializedName("source")
    public int source;

    @SerializedName("source_name")
    public String sourceName;

    // 证件类型，1：身份证
    @SerializedName("id_card")
    public int idCard;

    // 证件号码
    @SerializedName("id_code")
    public String idCode;

    // 邮箱地址
    @SerializedName("email")
    public String email;

    // 所在省份
    @SerializedName("province")
    public String province;
    // 所在城市
    @SerializedName("city")
    public String city;

    //是否置顶 0为没置顶, 1为置顶
    public int top;

    //置顶时间
    public long topTime;

    // 最近跟进记录的id
    public Long followId;

    /*******************
     * 非数据库
     ********************/
    //生日倒计时
    @SerializedName("difference")
    public int difference = -1;

    //保单信息列表
    @SerializedName("policies")
    public List<Insurance> policies;

    //标签列表
    @SerializedName("labels")
    public List<CustomTagData> labels;

    //客户保单数
    @SerializedName("policy_count")
    public int policy_count;

    // 计划书数
    @SerializedName("proposal_count")
    public int proposal_count;

    // 地址
    @SerializedName("address")
    public String address;

    // 备注
    @SerializedName("remark")
    public String remark;

    // 最新保单
    @SerializedName("lastest_policy")
    public Insurance lastest_policy;

    // 最新计划书
    @SerializedName("lastest_proposal")
    public Proposal lastest_proposal;

    //本地 是否已导入过
    public boolean isInput;
    //本地
    public long contanct_id;

    // 导入客户中判断是否选中，临时变量
    public boolean isSelect;

    /**
     * Used to resolve relations
     */
    private transient DaoSession daoSession;

    /**
     * Used for active entity operations.
     */
    private transient CustomerDao myDao;

    @SerializedName("follows")
    public LastFollow lastFollow;

    private Long lastFollow__resolvedKey;

    @SerializedName("contact_infos")
    public List<ContactInfo> contactInfos;

    public Custom() {
    }

    public Custom(Long customerId) {
        this.customerId = customerId;
    }

    public Custom(Long customerId, String name, Integer gender, String avatar, String ssoId, Integer birthdayType, String birthday,
                  Integer type, Integer category, String label, Integer source, String sourceName, Integer idCard, String idCode,
                  String email, String province, String city, Integer top, Long topTime, String pingyinName, Long followId) {
        this.customerId = customerId;
        this.name = name;
        this.gender = gender;
        this.avatar = avatar;
        this.ssoId = ssoId;
        this.birthdayType = birthdayType;
        this.birthday = birthday;
        this.type = type;
        this.category = category;
        this.label = label;
        this.source = source;
        this.sourceName = sourceName;
        this.idCard = idCard;
        this.idCode = idCode;
        this.email = email;
        this.province = province;
        this.city = city;
        this.top = top;
        this.topTime = topTime;
        this.followId = followId;
        initPingyingName();
    }

    /* 更新客户用 */
    public Custom(String real_name, long id, String birthday, int birthType, List<ContactInfo> contacts, String agent_id, int type,
                  int gender, int category, List<CustomTagData> labels, String label, String email, int id_card, String id_code,
                  String province, String city, String address, String remark) {
        this.customerId = id;
        this.name = real_name;
        this.birthday = birthday;
        this.birthdayType = birthType;
        this.contactInfos = contacts;
        this.ssoId = agent_id;
        this.type = type;
        this.gender = gender;
        this.category = category;
        this.labels = labels;
        this.label = label;
        this.email = email;
        this.idCard = id_card;
        this.idCode = id_code;
        this.province = province;
        this.city = city;
        this.address = address;
        this.remark = remark;
    }

    /* 新增客户用 */
    public Custom(String real_name, String birthday, int birthType, List<ContactInfo> contacts, int source,
                  String agent_id, int type, int gender, int category, String email, int id_card, String id_code,
                  String province, String city, String address, String remark) {

        this.name = real_name;
        this.birthday = birthday;
        this.birthdayType = birthType;
        this.contactInfos = contacts;
        this.source = source;
        this.ssoId = agent_id;
        this.type = type;
        this.gender = gender;
        this.category = category;
        this.email = email;
        this.idCard = id_card;
        this.idCode = id_code;
        this.province = province;
        this.city = city;
        this.address = address;
        this.remark = remark;
    }

    public void initPingyingName() {

    }

    /**
     * called by internal mechanisms, do not call yourself.
     */
    public void __setDaoSession(DaoSession daoSession) {
        this.daoSession = daoSession;
        myDao = daoSession != null ? daoSession.getCustomerDao() : null;
    }

    /**
     * To-one relationship, resolved on first access.
     */
    public LastFollow getLastFollow() {
        Long __key = this.followId;
        if (lastFollow__resolvedKey == null || !lastFollow__resolvedKey.equals(__key)) {
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            LastFollowDao targetDao = daoSession.getLastFollowDao();
            LastFollow lastFollowNew = targetDao.load(__key);
            synchronized (this) {
                lastFollow = lastFollowNew;
                lastFollow__resolvedKey = __key;
            }
        }
        return lastFollow;
    }

    public void setLastFollow(LastFollow lastFollow) {
        synchronized (this) {
            this.lastFollow = lastFollow;
            followId = lastFollow == null ? null : lastFollow.getFollowId();
            lastFollow__resolvedKey = followId;
        }
    }

    /**
     * To-many relationship, resolved on first access (and after reset). Changes to to-many relations are not persisted, make changes to the target entity.
     */
    public List<ContactInfo> getContactInfos() {
        if (contactInfos == null) {
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            ContactInfoDao targetDao = daoSession.getContactInfoDao();
            List<ContactInfo> contactInfosNew = targetDao._queryCustom_ContactInfos(customerId);
            synchronized (this) {
                if (contactInfos == null) {
                    contactInfos = contactInfosNew;
                }
            }
        }
        return contactInfos;
    }

    public synchronized void resetContactInfos() {
        contactInfos = null;
    }

    public void delete() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.delete(this);
    }

    public void update() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.update(this);
    }


    public void refresh() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.refresh(this);
    }

    // KEEP METHODS - put your custom methods here
    public Custom(String name, String birthday) {
        this.name = name;
        this.birthday = birthday;
    }

    public boolean hasTop() {
        return top == TAG_HAS_TOP;
    }

    /**
     * 根据IdCard代码获取显示字样
     *
     * @param idCard
     * @param context
     * @return
     */
    public static String getIdCardStr(int idCard, Context context) {
        switch (idCard) {
            case CUSTOM_IDCODE_ID:
                return context.getString(R.string.custom_idcode_id);
            case CUSTOM_IDCODE_PASSPORT:
                return context.getString(R.string.custom_idcode_passport);
            case CUSTOM_IDCODE_OFFICER:
                return context.getString(R.string.custom_idcode_officer);
            case CUSTOM_IDCODE_HKTAWIN:
                return context.getString(R.string.custom_idcode_hktaiwan);
            case CUSTOM_IDCODE_ELSE:
                return context.getString(R.string.custom_idcode_else);
            case CUSTOM_IDCODE_PERMANENT:
                return context.getString(R.string.custom_idcode_permanent);
            case CUSTOM_IDCODE_SOLDIER:
                return context.getString(R.string.custom_idcode_soldier);
            case CUSTOM_IDCODE_DIREVER:
                return context.getString(R.string.custom_idcode_driever);
            case CUSTOM_IDCODE_HOME:
                return context.getString(R.string.custom_idcode_home);
            case CUSTOM_IDCODE_ORGANIZATION:
                return context.getString(R.string.custom_idcode_organzation);
            case CUSTOM_IDCODE_HKPASSPORT:
                return context.getString(R.string.custom_idcode_hkpassport);
            case CUSTOM_IDCODE_TAIWANPASSPORT:
                return context.getString(R.string.custom_idcode_taiwanpassport);
            default:
                return context.getString(R.string.custom_idcode_num);
        }
    }

    /**
     * 根据已有的总tag数据生成改客户的tag数据
     *
     * @param tagAll
     * @return
     */
    public List<CustomTagData> getTags(List<CustomTagData> tagAll) {
        List<CustomTagData> tagDatas = new ArrayList<>();

        if (label == null || label.equals("null") || StringUtils.isEmpty(label))
            return tagDatas;

        List<String> labels = Arrays.asList(label.trim().split(","));
        if (tagAll != null) {
            for (String label : labels) {
                for (CustomTagData data : tagAll) {
                    try {
                        if (data.getId() == Integer.valueOf(label))
                            tagDatas.add(data);
                    } catch (NumberFormatException e) {
                    }
                }
            }
        }
        return tagDatas;
    }
    // KEEP METHODS END
}
